package com.azxc.rapid.auth.granter;

import com.azxc.rapid.core.redis.cache.RapidRedis;
import com.azxc.rapid.core.social.props.SocialProperties;
import com.azxc.rapid.supervise.feign.ISuperviseClient;
import com.azxc.rapid.system.user.feign.IUserClient;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.CompositeTokenGranter;
import org.springframework.security.oauth2.provider.TokenGranter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 自定义拓展TokenGranter
 *
 *
 */
public class RapidTokenGranter {

	private static String appKey = "APPAD3BADB33B0622F1BE4C0E5A";
	private static String	corpId = "787987360344064";
	private static String  corpSecret = "a1024mo37JlrK3u2_MR7yNv8rKXsGD819_sNsrJZ4Ubg0zSi5y_0k568xPRQ7TIe951";

	private static String clientId = "ornch0wwwelfrzb6e0gwl39uh340kxhp";

	private static String clientSecret = "rhslr1i117aurk1jnyllkhbk6xx7loq8s4me2446";

	//private static String portalUrl = "http://192.168.8.92:8108";
	//private static String portalUrl = "http://47.102.102.159:81/api/rapid-portal";

	//	private static String redirectUri = "http://192.168.8.91:81/saber/";
	//private static String redirectUri = "http://47.102.102.159:81/supervise";

	/**
	 * 自定义tokenGranter
	 */
	public static TokenGranter getTokenGranter(final AuthenticationManager authenticationManager, final AuthorizationServerEndpointsConfigurer endpoints, RapidRedis rapidRedis, IUserClient userClient, SocialProperties socialProperties, ISuperviseClient superviseClient
		,String clientId,String clientSecret,String portalUrl,String redirectUri) {
		// 默认tokenGranter集合
		List<TokenGranter> granters = new ArrayList<>(Collections.singletonList(endpoints.getTokenGranter()));
		// 增加验证码模式
		granters.add(new CaptchaTokenGranter(authenticationManager, endpoints.getTokenServices(), endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory(), rapidRedis));
		// 增加第三方登陆模式
		granters.add(new SocialTokenGranter(endpoints.getTokenServices(), endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory(), userClient, socialProperties));

		granters.add(new SuperviseTokenGranter(authenticationManager, endpoints.getTokenServices(), endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory(), rapidRedis,superviseClient));

		//增加自定义sso登录
		granters.add(new SSoTokenGranter(authenticationManager, endpoints.getTokenServices(), endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory(), rapidRedis,userClient));

		//增加卫士通单点登录
		granters.add(new WestoneGranter(endpoints.getTokenServices(), endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory(),userClient, appKey,corpId,corpSecret));

		//增加统一门户单点登录
		granters.add(new PortalGranter(endpoints.getTokenServices(), endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory(),userClient, clientId,clientSecret,portalUrl,redirectUri));

		//增加卫士通单点登录
		granters.add(new ShowGranter(endpoints.getTokenServices(), endpoints.getClientDetailsService(), endpoints.getOAuth2RequestFactory(),userClient));


		// 组合tokenGranter集合
		return new CompositeTokenGranter(granters);
	}

}
